package com.example.android.ui.home.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.android.R;
import com.example.android.bean.Goods;
import com.example.android.ui.MainActivity;
import com.example.android.ui.home.HomeFragment;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.adapter.BannerImageAdapter;
import com.youth.banner.holder.BannerImageHolder;
import com.youth.banner.indicator.CircleIndicator;

import org.w3c.dom.Text;

import java.util.List;

public class HomeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private RecyclerView recyclerView;
    private LayoutInflater layoutInflater;
    private Context context;
    private List<Goods> goodsList;
    private OnItemClickListener listener;

    public HomeRecyclerViewAdapter(RecyclerView recyclerView, Context context, List<Goods> goodsList) {
        this.recyclerView = recyclerView;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        this.goodsList = goodsList;

    }

    public void setGoods(List<Goods> goodsList) {
        this.goodsList.clear();
        this.goodsList.addAll(goodsList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        Log.i("-----", viewType + "");
        View view = layoutInflater.inflate(viewType, parent, false);
        if (viewType != R.layout.home_text && viewType != R.layout.home_image) {
            view.setOnClickListener(this);
        }
        if (viewType == R.layout.home_text_image) {
            return new MultiViewHolder(view);
        }
        return new SingleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Goods goods = goodsList.get(position);
        int itemViewType = getItemViewType(position);
        switch (itemViewType) {
            case R.layout.home_banner:
//                Log.i("-----", "banner" + position);
                ((Banner) holder.itemView).setAdapter(new BannerImageAdapter<String>(goods.getBanners()) {
                    @Override
                    public void onBindView(BannerImageHolder holder, String data, int position, int size) {
                        //图片加载自己实现
                        Glide.with(holder.itemView)
                                .load(data)
                                .apply(RequestOptions.centerCropTransform())
                                .into(holder.imageView);
                    }
                }).addBannerLifecycleObserver((LifecycleOwner) context)//添加生命周期观察者
                        .setIndicator(new CircleIndicator(context));
                break;
            case R.layout.home_image:
//                Log.i("-----", goods.getImageUrl() + position);
                Glide.with(holder.itemView)
                        .load(goods.getImageUrl())
                        .apply(RequestOptions.centerCropTransform())
                        .into((ImageView) holder.itemView);
                break;
            case R.layout.home_text:
//                Log.i("-----", goods.getText() + position);
                ((TextView) holder.itemView).setText(goods.getText());
                break;
            case R.layout.home_text_image:
//                Log.i("-----", goods.getImageUrl() + position);
                MultiViewHolder multiViewHolder = (MultiViewHolder) holder;
                multiViewHolder.textView.setText(goods.getText());
                multiViewHolder.price.setText(goods.getPrice()+"");
                Glide.with(context)
                        .load(goods.getImageUrl())
                        .apply(RequestOptions.centerCropTransform())
                        .into(multiViewHolder.imageView);
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        Goods goods = goodsList.get(position);
        if (goods.getBanners() != null) {
            //显示banner
            return R.layout.home_banner;
        } else if (goods.getImageUrl() == null) {
            //显示文字
            return R.layout.home_text;
        } else if (goods.getText() == null) {
            //显示图片
            return R.layout.home_image;
        } else {
            //显示文字+图片
            return R.layout.home_text_image;
        }
    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }

    @Override
    public void onClick(View view) {
        if (listener != null) {
            int position = recyclerView.getChildAdapterPosition(view);
            listener.onItemClick(goodsList.get(position));
        }
    }

    class SingleViewHolder extends RecyclerView.ViewHolder {
        public SingleViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    class MultiViewHolder extends RecyclerView.ViewHolder {
        TextView textView,price;
        ImageView imageView;
        public MultiViewHolder(@NonNull View itemView) {
            super(itemView);
            price =itemView.findViewById(R.id.home_text_image_price);
            textView = itemView.findViewById(R.id.home_text_image_text);
            imageView = itemView.findViewById(R.id.home_text_image_image);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(Goods goods);
    }

}
